import Vue from 'vue'
import Vuex from 'vuex'
import config from '@/env/config';
import {
    getUser,
    getCommunityList
} from '@/api/user';
import {
    getRolesList
} from '@/api/common'
// import {
//     getWxConfig
// } from '@/api/wechat'
Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        wxflag: false,
        user: {},
        token: {},
        community: {},
        rolesList: {},
        website: config,
    },
    getters: {
        roles: (state) => {
            return Number((state.user || {}).userType || 0)
        },
        isWuye: (state, getters) => {
            return state.rolesList.YZ !== getters.roles;
        },
        address: (state) => {
            const userInfo = state.user;
            const address = `${userInfo.communityName || ''}${userInfo.buildingName || ''}${userInfo.unitName || ''}${userInfo.houseName || ''}`;
            return address;
        }
    },
    mutations: {
        setRolesList(state, rolesList) {
            state.rolesList = rolesList;
        },
        setWxflag(state, wxflag) {
            state.wxflag = wxflag;
        },
        setToken(state, token) {
            state.token = token;
        },
        setUser(state, user) {
            state.user = user;
        },
        setCommunity(state, community) {
            state.community = community;
        }
    },
    actions: {
        getRolesList({
            commit
        }) {
            return new Promise((resolve) => {
                getRolesList().then(res => {
                    const data = res.data.data;
                    commit('setRolesList', data);
                    resolve();
                })
            })
        },
        // getWxConfig({
        //     commit
        // }) {
        //     return new Promise((resolve) => {
        //         getWxConfig({
        //             url: location.href.split('#')[0]
        //         }).then(res => {
        //             const data = res.data;
        //             window.wx.config({
        //                 debug: false,
        //                 appId: data.appId,
        //                 timestamp: data.timestamp,
        //                 nonceStr: data.nonceStr,
        //                 signature: data.signature,
        //                 jsApiList: config.jsApiList
        //             });
        //             commit('setWxflag', true);
        //             resolve();
        //         }).catch(() => {
        //             resolve();
        //         })
        //     });
        // },
        getCommunityList() {
            return new Promise((resolve) => {
                getCommunityList().then(res => {
                    const data = res.data;
                    const community = data.attributes.default_community;
                    resolve(community)
                })
            });
        },
        getUser({
            commit,
            getters,
            dispatch
        }) {
            return new Promise((resolve, reject) => {
                let data;
                getUser().then(res => {
                    data = res.data.obj;
                    commit('setUser', data)
                    return dispatch('getRolesList')
                }).then(() => {
                    if (getters.isWuye) {
                        dispatch('getCommunityList').then((community) => {
                            commit('setCommunity', {
                                communityId: community.communityId,
                                communityName: community.communityName
                            })
                            resolve();
                        })
                    } else {
                        commit('setCommunity', {
                            communityId: data.communityId,
                            communityName: data.communityName
                        })
                        resolve();
                    }
                }).catch(err => {
                    reject(err);
                })
            })

        }
    }
})